package com.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.gulimall.gulimallproduct.entity.ProductAttrValueEntity;
import com.gulimall.gulimallproduct.service.ProductAttrValueService;
import com.gulimall.gulimallproduct.vo.AttrRespVo;
import com.gulimall.gulimallproduct.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.gulimallproduct.entity.AttrEntity;
import com.gulimall.gulimallproduct.service.AttrService;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;



/**
 * 商品属性
 *
 * @author niujiaxing
 * @email sunlightcs@gmail.com
 * @date 2025-05-08 21:33:57
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;
    /**
     * 23、修改商品规格
     */
    @PostMapping("/update/{spuId}")
    public R UpdateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entityList){
       productAttrValueService.updateSpuAttr(spuId,entityList);
        return R.ok();
    }
    /**
     * 22、获取spu规格
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entityList=productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data",entityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    ///product/attr/base/list/{catelogId}

    /**
     * 获取分类规格参数
     * @param catelogId
     * @param params
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@PathVariable("catelogId") Long catelogId,
                          @RequestParam Map<String, Object> params,
                          @PathVariable("attrType") String type){
        PageUtils page =attrService.QueryAttrBasePage(params,catelogId,type);

        return R.ok().put("page",page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")

    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
